#include <QMessageBox>

#include "index.h"
#include "reshandler.h"
#include "client.h"

ResHandler::ResHandler(QObject *parent) : QObject(parent)
{

}

ResHandler::~ResHandler()
{
//    delete pdu;
}

void ResHandler::regist()
{
    bool ret;
    memcpy(&ret, pdu->caData, sizeof(bool));
    if(ret)
    {
        QMessageBox::information(&Client::getInstance(), "提示", "注册成功");
    }
    else
    {
        QMessageBox::information(&Client::getInstance(), "提示", "注册失败: 用户名或密码非法");
    }
}

void ResHandler::login()
{
    bool ret;
    memcpy(&ret, pdu->caData, sizeof(bool));
    if(ret)
    {
        QMessageBox::information(&Client::getInstance(), "提示", "登录成功");
        Index::getInstance().show();
        Client::getInstance().hide();
    }
    else
    {
        QMessageBox::information(&Client::getInstance(), "提示", "登录失败: 用户名或密码错误");
    }
}

void ResHandler::findUser()
{
    int ret;
    memcpy(&ret, pdu->caData, 32);
    if(ret == -1)
    {
        QMessageBox::information(&Index::getInstance(), "提示", "该用户不存在");
    }
    else if(ret == 0)
    {
        QMessageBox::information(&Index::getInstance(), "提示", "该用户不在线");
    }
    else if(ret == 1)
    {
        int res = QMessageBox::information(&Index::getInstance(), "提示", "该用户在线", "添加好友", "取消");
        if(res == 0){
            QString strCurName = Client::getInstance().m_strLoginName;

            char caTarName[32] = { '\0' };
            memcpy(caTarName, pdu->caData + 32, 32);

            PDU* pdu = mkPDU(ENUM_MSG_TYPE_ADD_FRIEND_REQUEST, 0);
            memcpy(pdu->caData     , strCurName.toStdString().c_str(), 32);
            memcpy(pdu->caData + 32, caTarName                       , 32);

            Client::getInstance().sendMsg(pdu);
        }
    }
}

void ResHandler::onlineUser(QString m_strLoginName)
{
    QStringList userlist;
    char userName[32];
    uint len = pdu->uiMsgLen / 32;
    for(uint i = 0; i < len; i++){
        memcpy(userName, pdu->caMsg + i * 32, 32);

        if(QString(userName) == m_strLoginName) continue;

        userlist.append(QString(userName));

    }
    Index::getInstance().getFriend()->getOnlineUser()->updateOnlineUserWigetList(userlist);
}

void ResHandler::addFriend()
{
    int ret;
    memcpy(&ret, pdu->caData, sizeof(ret));
    if(ret == -2)
    {
        QMessageBox::information(&Index::getInstance(), "提示", "对方已是好友");
    }
    else if(ret == -1)
    {
        QMessageBox::information(&Index::getInstance(), "提示", "出现内部错误");
    }
    else if(ret == 0)
    {
        QMessageBox::information(&Index::getInstance(), "提示", "对方不在线");
    }
}

void ResHandler::addFriendRequest()
{
    char caCurName[32] = { '\0' };
    memcpy(caCurName, pdu->caData, 32);
    int ret = QMessageBox::question(&Index::getInstance(), "提示", QString("是否同意 '%1' 的好友请求？").arg(caCurName));
    if(ret != QMessageBox::Yes) return;
    PDU* respdu = mkPDU(ENUM_MSG_TYPE_ADD_FRIEND_AGREE_REQUEST, 0);
    memcpy(respdu->caData, pdu->caData, 64);
    Client::getInstance().sendMsg(respdu);

}

void ResHandler::addFriendAgree()
{
    QMessageBox::information(&Index::getInstance(), "提示", "添加成功");
    Index::getInstance().getFriend()->flushFriend();
}

void ResHandler::flushFriend()
{
    QStringList friendlist;
    char userName[32];
    uint len = pdu->uiMsgLen / 32;
    for(uint i = 0; i < len; i++){
        memcpy(userName, pdu->caMsg + i * 32, 32);
        friendlist.append(QString(userName));
    }
    Index::getInstance().getFriend()->updateFriendWigetList(friendlist);
}

void ResHandler::delFriend()
{
    int ret;
    memcpy(&ret, pdu->caData, sizeof(ret));
    if(ret == 1)
    {
        QMessageBox::information(&Index::getInstance(), "提示", "删除成功");
    }
    else if(ret == -1)
    {
        QMessageBox::information(&Index::getInstance(), "提示", "对方不是好友");
    }
    else if(ret == 0)
    {
        QMessageBox::information(&Index::getInstance(), "提示", "内部错误");
    }
    Index::getInstance().getFriend()->flushFriend();
}

void ResHandler::chatRequest()
{
    Chat* c = Index::getInstance().getFriend()->m_pChat;
    if(c->isHidden()){
        c->show();
    }

    char caChatName[32] = { '\0' };
    memcpy(caChatName, pdu->caData, 32);
    if(QString(caChatName) != Client::getInstance().m_strLoginName){
        c->setChatName(caChatName);
    }
    c->updateShowContent(QString("%1: %2").arg(caChatName).arg((char*)pdu->caMsg));



}

void ResHandler::mkDir()
{
    bool ret;
    memcpy(&ret, pdu->caData, sizeof(bool));
    if(ret){
        QMessageBox::information(&Index::getInstance(), "提示", "文件夹创建成功");
    }else{
        QMessageBox::information(&Index::getInstance(), "提示", "文件夹创建失败");
    }
}
